from db import Db


class Page:
    def __init__(self,total=0,page=1,num=1):
        self.total = total
        self.page = page
        self.num = num
        if self.page <=0:
            self.page = 1
    #计算页数
    def page_num(self):
        if self.total % self.num == 0:
            return self.total // self.num
        else:
            return self.total // self.num+1
    
    #获取分页后数据
    def get_data(self,sql,sql2):
        #limit OFFSET (page-1)*num ,num 
        _limit = (self.page-1)*self.num
        _offset = self.num
        sql1 =sql2+" limit %s,%s"%(_limit,_offset)
        #执行sql
        sql+=sql1
        mydb = Db()
        res = mydb.get_all(sql)
        return res

# page = Page(6,1,3)
# print(page.get_data("select * from `course` where uid=45 order by id desc"))










